<?php 
/* this file is supposed to handle errors and display user friendly error messages */
// load wordpress defaults, make global variables/functions available
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];
require( $this_file . 'wp-blog-header.php' );
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>

<!-- head start -->

<head profile="http://gmpg.org/xfn/11">
	<?php
    $aOptions = SofaSuppaStore::initOptions( false );
	// handle font selection
	$google_fonts = $aOptions[ 'google_fonts' ];
	$font_str = $google_fonts;
	$font_str_arr = explode( ':', $font_str );
	$font_name = $font_str_arr[ 0 ];
	$clean_font_arr = explode( '+', $font_name );
	$clean_font_name = implode( ' ', $clean_font_arr );
    ?>
    <link href='http://fonts.googleapis.com/css?family=<?php echo $google_fonts; ?>' rel='stylesheet' type='text/css' />
    <link rel="stylesheet" href="<?php echo( get_template_directory_uri() . '/960.css' ); ?>" type="text/css" media="screen, projection" />
    <link rel="stylesheet" href="<?php echo( get_template_directory_uri() . '/screen.css' ); ?>" type="text/css" media="screen, projection" />
    <style type="text/css">
	/* handle basic styles according to google fonts selection */
	h1, h2, h3, h4, h5, h6 { font-family: '<?php echo $clean_font_name; ?>', Arial, sans-serif; font-weight: normal; }
	</style>
	<link rel="stylesheet" href="<?php bloginfo( 'stylesheet_url' ); ?>" type="text/css" media="screen, projection" />
    
    <?php wp_print_scripts( 'jquery' ); ?>
</head>

<!-- head end -->

<?php
// error codes (localizable)
$err_msg_1 = __( "Shipping country not selected! We must know your residence country in order to be able to make delivery.", "sofa_suppastore" );
$err_msg_2 = __( "Your shopping basket is empty!", "sofa_suppastore" );
?>

<body>

<!-- main content start -->

<div id="lightcontent" class="container_8 clearfix">
    
	<?php
	// get error details
	if( $_SERVER[ 'REQUEST_METHOD' ] == 'GET' ) {
		$errorcode  = !empty( $_GET[ 'errorcode' ] ) ? stripslashes( strip_tags( $_GET[ 'errorcode' ] ) ) : NULL; // product size
	} elseif( $_SERVER[ 'REQUEST_METHOD' ] == 'REQUEST' ) {
		$errorcode  = !empty( $_REQUEST[ 'errorcode' ] ) ? stripslashes( strip_tags( $_REQUEST[ 'errorcode' ] ) ) : NULL; // product size
	}
	?>

    <div id="featured" class="grid_8">
    
    	<!-- title start -->
    
        <h2 class="grid_8 alpha omega"><font style="color: #ED1C24;"><?php _e( "Warning!", "sofa_suppastore" ); ?></font></h2>
        
        <!-- title end -->
        
        <!-- content start -->
        
        <div id="proddetails" class="grid_8 alpha omega">
        
			<?php 
			if( is_numeric( $errorcode ) ) {
				if( intval( $errorcode ) == 1 ) echo '<p>' . $err_msg_1 . '</p>';
				elseif( intval( $errorcode ) == 2 ) echo '<p>' . $err_msg_2 . '</p>';
			} else {
				// otherwise it comes from stock check
				$arr_critical = explode( '^', $errorcode );
				array_pop( $arr_critical ); // remove last empty
				$arr_critical_len = count( $arr_critical ); // how many items?
				
				if( $arr_critical ) {
					$encoded_errorcode = urlencode( $errorcode );
					
			?>
            
				<script type='text/javascript'>
                    jQuery( document ).ready( function() {
                        var decoded_errorcode = decodeURIComponent( "<?php echo $encoded_errorcode; ?>" );
                        decoded_errorcode = decoded_errorcode.replace( /\+/g, ' ' );
                        jQuery( '#errcode' ).val( decoded_errorcode );
                        jQuery( '#cleanup' ).click( function() {
                            jQuery( '#cleaner' ).submit();
                            return false;
                        } );
                    } );
                </script>
    
    		<?php
			
				}
				
				echo '<p>' . __( "The following items in your shopping basket are not available (stock too low)!", "sofa_suppastore" ) . '</p>';
				echo '<table id="tblmeta" width="100%" border="0" cellspacing="0" cellpadding="0">';
				echo '<th scope="col" class="textleft">' . __( "Product Name", "sofa_suppastore" ) . '</th>';
				echo '<th scope="col" class="textright">' . __( "Stock", "sofa_suppastore" ) . '</th>';
				
				if( $arr_critical ) {
					
					$z = 0;
					while( $z < $arr_critical_len ) {
						$arr_temp = explode( '~', $arr_critical[ $z ] );
						
						// make the list
						$product_id     = intval( $arr_temp[ 0 ] );
						$product_stock  = intval( $arr_temp[ 1 ] );
						
						echo '<tr>';
						echo '<td class="textleft">' . get_the_title( $product_id ) . '</td>';
						echo '<td class="textright">' . $product_stock . '</td>';
						echo '</tr>';
						
						$z ++;
					}

					echo '<tr>';
					echo '<td colspan="2" class="textright">';
					echo '<button id="cleanup">' . __( "Cleanup Basket?", "sofa_suppastore" ) . '</button>';
					echo '</td>';
					echo '</tr>';
					
				} else {
					
					echo '<tr>';
					echo '<td colspan="2" class="textright">';
					echo __( "Error occured! Not enough information.", "sofa_suppastore" );
					echo '</td>';
					echo '</tr>';
					
				}
				
				echo '</table>';
			?>
            
            <form id="cleaner" style="padding: 0; margin: 0;" method="post" action="<?php echo get_template_directory_uri() . '/cart_handler.php'; ?>">
            	<input type="hidden" name="dothis" id="dothis" value="cleanup" />
            	<input type="hidden" name="errcode" id="errcode" value="" />
                <?php wp_nonce_field( 'chck_ref_cc', 'sofa_suppastore_cc' ); ?>
            </form>
            
            <?php
			} // end stock check error handler
			?>
        
        </div>
        
        <!-- content end -->
        
    </div>
        
</div>

<!-- main content end -->

</body>
</html>